package com.citms.mq;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.PascalNameFilter;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.citms.remote.model.RabbitMQConfigModel;
import lombok.Data;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.amqp.RabbitProperties;
import org.springframework.stereotype.Component;

@Data
@Component
public class MessageQueueUtils {

	@Autowired
	private RabbitTemplate template;

	@Autowired
	private RabbitProperties rabbitProperties;

	@Autowired
	private RabbitMQConfigModel rabbitMQConfig;

	@Autowired
	private RoutingKeyProperties routingKeyProperties;

	
	public void sendFrontDeviceMsg(String message) {
		template.convertAndSend(rabbitMQConfig.getExchangeName(), routingKeyProperties.getFrontDevice(), message);
	}
	
	public void sendSpottingMsg(String message) {
		template.convertAndSend(rabbitMQConfig.getExchangeName(), routingKeyProperties.getSpotting(), message);
	}
	
	public void sendFrontDeviceMsg(MsgDecorator<?> msg) {
		this.sendFrontDeviceMsg(convertMsg(msg));
	}

	public void sendSpottingMsg(MsgDecorator<?> msg) {
		this.sendSpottingMsg(convertMsg(msg));
	}
	
	public String convertMsg(MsgDecorator<?> msg) {
		String message = "";
		// 为了把空值展示出来 首字母大写
		message = JSON.toJSONString(msg, new PascalNameFilter(),SerializerFeature.WriteMapNullValue,
				SerializerFeature.WriteNullStringAsEmpty);

		return message;
	}
	
	
}
